package com.home.demo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.home.demo.common.PageUtils;
import com.home.demo.common.R;
import com.home.demo.entity.UserEntity;
import com.home.demo.entity.dto.user.UserAndRoleSaveDTO;
import com.home.demo.entity.dto.user.UserUpdateDTO;
import com.home.demo.entity.vo.user.UserListVO;
import com.home.demo.entity.vo.user.UserLoginVO;
import com.home.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 
 *
 * @author ${author}
 * @email ${email}
 * @date 2024-02-21 15:24:19
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     *
     */
    @GetMapping("getCareList")
    public R getCareList(){
        List<UserEntity> u =  userService.getCareList();
       return R.ok().put("data",u);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = userService.queryPage(params);

        return R.ok().put("data", page);
    }

    /**
     * 角色列表
     */
    @GetMapping("getUserList")
    private R getUserList(@RequestParam Map<String,Object> params){
        IPage<UserListVO> selectPageVo =  userService.getUserList(params);
        return R.ok().put("data",selectPageVo);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{username}")
    public R info(@PathVariable("username") String username){
		UserLoginVO user = userService.selectUserInfo(username);

        return R.ok().put("data", user);
    }

    /**
     * 注册账号
     */
    @RequestMapping("/save")
    public R save(@RequestBody UserAndRoleSaveDTO user){
        boolean b = userService.saveInfo(user);
        return b?R.ok():R.error("账号已存在");
    }

    /**
     * 修改用户信息 && 修改密码
     *
     */
    @RequestMapping("/update")
    public R update(@RequestBody UserUpdateDTO user){
        UserEntity userEntity = userService.updateByIdOrUpdatePassword(user);

        return userEntity!=null?R.ok().put("data",userEntity):R.error("校验旧密码失败");
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] ids){
		userService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }



}
